package com.ys.web.service.impl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.web.dao.CommonDao;
import com.ys.web.entity.CustomerTarget;
import com.ys.web.entity.IndustryGoal;
import com.ys.web.entity.OriginBaidu;
import com.ys.web.entity.User;
import com.ys.web.pojo.CompanyInfo;
import com.ys.web.pojo.TotalNotGoal;
import com.ys.web.remotemodel.RemoteIndustryInfo;
import com.ys.web.service.CompanyService;
import com.ys.web.service.IndustryGoalService;
import com.ys.web.ws.service.ManageIndustryService;

/**
 * 
 * @author 卓小建
 *
 */
@Service
@Transactional
public class IndustryGoalServiceImpl implements IndustryGoalService {

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private CompanyService companyService;
	
	@Resource
	private ManageIndustryService manageIndustryService;

	public SimpleDateFormat sdd = new SimpleDateFormat("yyyy-MM-dd");
	DecimalFormat dfd = new DecimalFormat("######0.00");

	@Override
	public List<IndustryGoal> getALLIndustryByPage(int currentPage) {
		// TODO Auto-generated method stub
		List<IndustryGoal> list = new ArrayList<>();

		List<RemoteIndustryInfo> remoteIndustryInfoList = manageIndustryService.getALLIndustryList();
		for (RemoteIndustryInfo industry : remoteIndustryInfoList) {
			IndustryGoal industryGoal = new IndustryGoal();
			industryGoal.setId(industry.getId());
			industryGoal.setAvgConsultGoal(0.0);
			industryGoal.setAvgConversionGoal(0.0);
			list.add(industryGoal);
		}
		return list;
	}

	@Override
	public void saveOrUpdate(IndustryGoal industry) {
		// TODO Auto-generated method stub
		commonDao.saveOrUpdate(industry);

	}

	@Override
	public <T> T getTById(Class<T> className, String Id) {
		// TODO Auto-generated method stub
		T in = commonDao.get(className, Id);

		return in;
	}

	@Override
	public <T> void save(T entity) {
		// TODO Auto-generated method stub
		commonDao.save(entity);
	}

	@Override
	public <T> T getEntityById(Class<T> className, Serializable id) {
		// TODO Auto-generated method stub
		return commonDao.get(className, id);
	}

	@Override
	public <T> List<T> getAllEntity(Class<T> className, Object object, Object object1, Object object2) {
		// TODO Auto-generated method stub
		String hql = "";
		if (("".equals(object2) || object2 == null) && (!"".equals(object1) || object1 != null)) {
			hql = " from " + className.getSimpleName() + " where companyId=" + object + " and promotionDate like '%"
					+ object1 + "%'";
		}
		if (("".equals(object1) || object1 == null) && (!"".equals(object1) || object1 != null)) {
			hql = " from " + className.getSimpleName() + " where companyId=" + object + " and promotionDate like '%"
					+ object2 + "%'";
		}
		if ((object1 != null && object2 != null) && (!"".equals(object1) && !"".equals(object2))) {
			hql = " from " + className.getSimpleName() + " where companyId=" + object + " and promotionDate between '"
					+ object1 + "' and '" + object2 + "'";
		}
		// System.out.println(hql);
		return commonDao.findByQueryString(hql);
	}

	@Override
	public <T> List<T> getT(Class<T> className) {
		// TODO Auto-generated method stub
		String hql = " from " + className.getSimpleName();
		return commonDao.findByQueryString(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getRemoteEntity(String id) {
		// TODO Auto-generated method stub
		T t = (T) manageIndustryService.getEntity(id);
		return t;
	}

	@Override
	public List<CustomerTarget> getCustomerGoal(List<CompanyInfo> list, String object, String object1) {
		// TODO Auto-generated method stub
		int sumTotalnumberConsult;
		double sumConsume;
		List<CustomerTarget> customerTarget = new ArrayList<CustomerTarget>();

		if (list != null) {
			for (CompanyInfo ci : list) {
				//System.out.println("状态："+ci.getCompanyStateValue()+"   广告上线日期："+ci.getAdOnlineDate());
				
				sumTotalnumberConsult = 0;
				sumConsume = 0.0;
				CustomerTarget ct = new CustomerTarget();
				ct.setCompanyId(ci.getId());
				ct.setIndustryId(ci.getIndustryId());
				ct.setIndustryName(ci.getIndustry());
				ct.setCompanyName(ci.getCompanyName());
				ct.setUserId(ci.getUserId());
				User ui = null;
				if (ci.getUserId() == null) {
					ui = null;
				} else {
					ui = getEntityById(User.class, ci.getUserId());
				}
				if (ui != null) {
					ct.setUserName(ui.getEmployeeName());
				} else {
					ct.setUserName("暂无");
				}
				IndustryGoal ig = getTById(IndustryGoal.class, ci.getIndustryId());
				if (ig != null) {
					ct.setAvgDayConsultGoal(ig.getAvgConsultGoal());
					ct.setAvgDayConversionCostGoal(ig.getAvgConversionGoal());
				} else {
					ct.setAvgDayConsultGoal(-2.0);
					ct.setAvgDayConversionCostGoal(-2.0);
				}
				List<OriginBaidu> originBaidu = null;
				if ("null".equals(object) && "null".equals(object1)) {
					originBaidu = getAllEntity(OriginBaidu.class, ci.getId(), getBeforeSimpleDate(), "");
				} else {
					originBaidu = getAllEntity(OriginBaidu.class, ci.getId(), object, object1);
				}

				if (originBaidu != null && originBaidu.size() != 0) {
					if ("null".equals(object) && "null".equals(object1)) {
						ct.setPromotionDate(getBeforeSimpleDate());
					} else if ("".equals(object) && !"".equals(object1)) {
						ct.setPromotionDate(object1);
					} else if ("".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object);
					} else if (!"".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object+" ~ "+object1);
					}
					/*
					 * for(OriginBaidu ob : originBaidu){ sumTotalnumberConsult
					 * = sumTotalnumberConsult+ob.getTotalnumberConsult();
					 * sumConsume = sumConsume+ob.getConsume(); }
					 */
					for (int j = 0; j < originBaidu.size(); j++) {
						sumTotalnumberConsult = sumTotalnumberConsult + originBaidu.get(j).getTotalnumberConsult();
						sumConsume = sumConsume + originBaidu.get(j).getConsume();
					}

					ct.setAvgDayConsult((double) sumTotalnumberConsult);

					if (sumTotalnumberConsult != 0) {
						double avgdayconversion = sumConsume / sumTotalnumberConsult;
						ct.setAvgDayConversionCost(Double.parseDouble(dfd.format(avgdayconversion)));
					} else {
						ct.setAvgDayConversionCost(-1.0);
					}
				} else {
					if ("null".equals(object) && "null".equals(object1)) {
						ct.setPromotionDate(getBeforeSimpleDate());
					} else if ("".equals(object) && !"".equals(object1)) {
						ct.setPromotionDate(object1);
					} else if ("".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object);
					} else if (!"".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object + " ~ " + object1);
					}
					ct.setAvgDayConsult(-2.0);
					ct.setAvgDayConversionCost(-2.0);
				}

				if (ct.getAvgDayConsult() >= ct.getAvgDayConsultGoal()) {
					ct.setIsConsultGoal("是");
				} else {
					ct.setIsConsultGoal("否");
				}
				if (ct.getAvgDayConversionCost() <= ct.getAvgDayConversionCostGoal()) {
					ct.setIsConversionGoal("是");
				} else {
					ct.setIsConversionGoal("否");
				}
				//过滤注销客户
				if(!"注销".equals(ci.getCompanyStateValue())){
					customerTarget.add(ct);
				}
			}
		} else {
			customerTarget = null;
		}
		return customerTarget;
	}

	public static String getBeforeSimpleDate() {

		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca_before = Calendar.getInstance();
		ca_before.set(Calendar.DATE, ca_before.get(Calendar.DATE) - 1);
		return sd.format(ca_before.getTime());
	}

	@Override
	public List<CustomerTarget> getCustomerNotGoal(List<CompanyInfo> list, String object, String object1) {
		// TODO Auto-generated method stub
		List<CustomerTarget> c = getCustomerGoal(list, object, object1);
		List<CustomerTarget> cc = new ArrayList<>();
		if (c != null) {
			for (CustomerTarget ct : c) {
				if (ct.getAvgDayConsult() < ct.getAvgDayConsultGoal()
						|| ct.getAvgDayConversionCost() > ct.getAvgDayConversionCostGoal()) {
					CustomerTarget ctt = new CustomerTarget();
					BeanUtils.copyProperties(ct, ctt);
					cc.add(ctt);
				}
			}
		} else {
			cc = null;
		}
		return cc;
	}

	@Override
	public List<TotalNotGoal> getCustomerTotalNotGoal( String object, String object1,Integer infopage) {
		// TODO Auto-generated method stub
		//System.out.println("getCustomerTotalNotGoal");
		int sumTotalnumberConsult = 0;
		double sumConsume = 0.0;
		List<CustomerTarget> customerTarget = new ArrayList<>();
		List<TotalNotGoal> tng = new ArrayList<>();
		List<CompanyInfo> companyInfos = null;
		List<User> users = getT(User.class);
		if (users != null) {
			for (int i = 0; i < users.size(); i++) {// for
				customerTarget = new ArrayList<CustomerTarget>();
				Integer useriid = users.get(i).getId();
				companyInfos = companyService.getRemoteCompanyBySemUserId_baidu(useriid, infopage);
				int count = 0;
				if (companyInfos != null) {
					
					for (CompanyInfo ci : companyInfos) {//for
						sumTotalnumberConsult = 0;
						sumConsume = 0.0;
						CustomerTarget ct = new CustomerTarget();
						ct.setCompanyId(ci.getId());
						ct.setIndustryId(ci.getIndustryId());
						ct.setIndustryName(ci.getIndustry());
						ct.setCompanyName(ci.getCompanyName());
						ct.setUserId(useriid);
						User ui = null;
						if (ct.getUserId() == null) {
							ui = null;
						} else {
							ui = getEntityById(User.class, ct.getUserId());
						}
						if (ui != null) {
							ct.setUserName(ui.getEmployeeName());
						} else {
							ct.setUserName("暂无");
						}
						IndustryGoal ig = getTById(IndustryGoal.class,
								ci.getIndustryId());
						if (ig != null) {
							ct.setAvgDayConsultGoal(ig.getAvgConsultGoal());
							ct.setAvgDayConversionCostGoal(ig.getAvgConversionGoal());
						} else {
							ct.setAvgDayConsultGoal(-2.0);
							ct.setAvgDayConversionCostGoal(-2.0);
						}

						// 获取百度咨询量和转化成本

						List<OriginBaidu> originBaidu = null;
						if ("null".equals(object) && "null".equals(object1)) {
							originBaidu = getAllEntity(OriginBaidu.class, ci.getId(),
									getBeforeSimpleDate(), "");
						} else {
							originBaidu = getAllEntity(OriginBaidu.class, ci.getId(),
									object, object1);
						}
						if (originBaidu != null && originBaidu.size() != 0) {
							for(int j=0;j<originBaidu.size();j++){
								sumTotalnumberConsult = sumTotalnumberConsult+originBaidu.get(j).getTotalnumberConsult();
								sumConsume = sumConsume+originBaidu.get(j).getConsume();
							}
							ct.setAvgDayConsult((double) sumTotalnumberConsult);
							if (sumTotalnumberConsult != 0) {
								double avgdayconversion = sumConsume / sumTotalnumberConsult;
								ct.setAvgDayConversionCost(Double.parseDouble(dfd.format(avgdayconversion)));
							} else {
								ct.setAvgDayConversionCost(-1.0);
							}
						} else {
							ct.setAvgDayConsult(-2.0);
							ct.setAvgDayConversionCost(-2.0);
						}
						if (ct.getAvgDayConsult() >= ct.getAvgDayConsultGoal()) {
							ct.setIsConsultGoal("是");
						} else {
							ct.setIsConsultGoal("否");
						}
						if (ct.getAvgDayConversionCost() <= ct.getAvgDayConversionCostGoal()) {
							ct.setIsConversionGoal("是");
						} else {
							ct.setIsConversionGoal("否");
						}
						if (ct.getAvgDayConsult() < ct.getAvgDayConsultGoal()
								|| ct.getAvgDayConversionCost() > ct.getAvgDayConversionCostGoal()) {
							if(!"注销".equals(ci.getCompanyStateValue())){
								count = count + 1;
								customerTarget.add(ct);
							}
							//customerTarget.add(ct);
						}
					}//for
				   

				}//if(companyInfos != null)
				 TotalNotGoal t = new TotalNotGoal();
			       t.setUserId(users.get(i).getId());
			       t.setUserName(users.get(i).getEmployeeName());
			       t.setCount(count);
			       tng.add(t);
			       t = null;
			count = 0;
			} // for(user)
			
		}
		return tng;
	}

	@Override
	public List<CustomerTarget> getCustomerSemNotGoal(List<CompanyInfo> list, String object, String object1,
			Integer userId) {
		// TODO Auto-generated method stub
		//System.out.println("getCustomerSemNotGoal");
		int sumTotalnumberConsult = 0;
		double sumConsume = 0.0;
		List<CustomerTarget> customerTarget = new ArrayList<CustomerTarget>();
		if (list != null) {
			for (CompanyInfo ci : list) {
				sumTotalnumberConsult = 0;
				sumConsume = 0.0;
				CustomerTarget ct = new CustomerTarget();
				ct.setCompanyId(ci.getId());
				ct.setIndustryId(ci.getIndustryId());
				ct.setIndustryName(ci.getIndustry());
				ct.setCompanyName(ci.getCompanyName());
				ct.setUserId(userId);
				User ui = null;
				if (ci.getUserId() == null) {
					ui = null;
				} else {
					ui = getEntityById(User.class, ci.getUserId());
				}
				if (ui != null) {
					ct.setUserName(ui.getEmployeeName());
				} else {
					ct.setUserName("暂无");
				}
				IndustryGoal ig = getTById(IndustryGoal.class, ci.getIndustryId());
				if (ig != null) {
					ct.setAvgDayConsultGoal(ig.getAvgConsultGoal());
					ct.setAvgDayConversionCostGoal(ig.getAvgConversionGoal());
				} else {
					ct.setAvgDayConsultGoal(-2.0);
					ct.setAvgDayConversionCostGoal(-2.0);
				}

				// 获取百度咨询量和转化成本

				List<OriginBaidu> originBaidu = null;
				if ("null".equals(object) && "null".equals(object1)) {
					originBaidu = getAllEntity(OriginBaidu.class, ci.getId(), getBeforeSimpleDate(), "");
				} else if ("".equals(object) && "".equals(object1)) {
					originBaidu = getAllEntity(OriginBaidu.class, ci.getId(), getBeforeSimpleDate(), "");
				} else {
					originBaidu = getAllEntity(OriginBaidu.class, ci.getId(), object, object1);
				}
				if (originBaidu != null && originBaidu.size() != 0) {
					if ("null".equals(object) && "null".equals(object1)) {
						ct.setPromotionDate(getBeforeSimpleDate());
					} else if ("".equals(object) && !"".equals(object1)) {
						ct.setPromotionDate(object1);
					} else if ("".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object);
					} else if (!"".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object+" ~ "+object1);
					}
					for (int j = 0; j < originBaidu.size(); j++) {
						sumTotalnumberConsult = sumTotalnumberConsult + originBaidu.get(j).getTotalnumberConsult();
						sumConsume = sumConsume + originBaidu.get(j).getConsume();
					}
					ct.setAvgDayConsult((double) sumTotalnumberConsult);
					if (sumTotalnumberConsult != 0) {
						double avgdayconversion = sumConsume / sumTotalnumberConsult;
						ct.setAvgDayConversionCost(Double.parseDouble(dfd.format(avgdayconversion)));
					} else {
						ct.setAvgDayConversionCost(-1.0);
					}
				} else {
					if ("null".equals(object) && "null".equals(object1)) {
						ct.setPromotionDate(getBeforeSimpleDate());
					} else if ("".equals(object) && !"".equals(object1)) {
						ct.setPromotionDate(object1);
					} else if ("".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object);
					} else if (!"".equals(object1) && !"".equals(object)) {
						ct.setPromotionDate(object + " ~ " + object1);
					}
					ct.setAvgDayConsult(-2.0);
					ct.setAvgDayConversionCost(-2.0);
				}
				if (ct.getAvgDayConsult() >= ct.getAvgDayConsultGoal()) {
					ct.setIsConsultGoal("是");
				} else {
					ct.setIsConsultGoal("否");
				}
				if (ct.getAvgDayConversionCost() <= ct.getAvgDayConversionCostGoal()) {
					ct.setIsConversionGoal("是");
				} else {
					ct.setIsConversionGoal("否");
				}
				if (ct.getAvgDayConsult() < ct.getAvgDayConsultGoal()
						|| ct.getAvgDayConversionCost() > ct.getAvgDayConversionCostGoal()) {
					if(!"注销".equals(ci.getCompanyStateValue())){
						customerTarget.add(ct);
					}
					
				}
			}
			//System.out.println(customerTarget.size());
		} else {
			customerTarget = null;
		}
		
		return customerTarget;
	}
}
